#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/11
# section 055
# 
# author: xiaosanyu
# website: yuxiaosan.tk \
#          http://blog.csdn.net/a87b01c14
# created: 16/7/11

from gi.repository import Gtk
import gi
TITLE = "ScrolledWindow"
DESCRIPTION = """
Gtk.ScrolledWindow is a Gtk.Bin subclass: it’s a container the accepts a single child widget.
Gtk.ScrolledWindow adds scrollbars to the child widget and optionally draws a beveled frame around the child widget.

The scrolled window can work in two ways. Some widgets have native scrolling support;
these widgets implement the Gtk.Scrollable interface. Widgets with native scroll support include Gtk.TreeView, Gtk.TextView, and Gtk.Layout.

For widgets that lack native scrolling support, the Gtk.Viewport widget acts as an adaptor class,
implementing scrollability for child widgets that lack their own scrolling capabilities.
Use Gtk.Viewport to scroll child widgets such as Gtk.Grid, Gtk.Box, and so on.

If a widget has native scrolling abilities, it can be added to the Gtk.ScrolledWindow
with Gtk.Container.add(). If a widget does not, you must first add the widget to a Gtk.Viewport,
then add the Gtk.Viewport to the scrolled window. Gtk.Container.add() will do this for you for widgets
that don’t implement Gtk.Scrollable natively, so you can ignore the presence of the viewport
"""


gi.require_version('Gtk', '3.0')

HEIGHT = 20
NUM = 200


class ScrolledWindow(Gtk.Window):
    def __init__(self):
        super(ScrolledWindow, self).__init__(title="ScrolledWindow Demo")
        self.set_size_request(200, 200)
        self.layout = Gtk.Layout.new(None, None)
        # important Sets the size of the scrollable area of the layout.
        self.layout.set_size(200, NUM * HEIGHT)
        self.sw = Gtk.ScrolledWindow()
        self.sw.set_shadow_type(Gtk.ShadowType.ETCHED_IN)
        self.sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
        self.sw.add(self.layout)
        for i in range(NUM):
            self.layout.put(Gtk.Label("label" + str(i)), 20, HEIGHT * i)

        self.add(self.sw)


def main():
    win = ScrolledWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()


if __name__ == "__main__":
    main()
